package com.hxx.common.core.wcontour.util;


import com.hxx.common.core.wcontour.base.TrainData;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.SchemaException;
import wcontour.Contour;
import wcontour.Interpolate;
import wcontour.global.Border;
import wcontour.global.PolyLine;
import wcontour.global.Polygon;

import java.io.IOException;
import java.util.List;

/**
 * 等值面分析工具类
 *
 * @author hxd
 */
public class FeatureSurfaceUtil {

    /**
     * 生成等值面
     *
     * @param dataInterval 数据分层级别
     */
    public FeatureCollection equiSurface(TrainData trainData, double[] dataInterval) throws SchemaException {
        // 等值点数据，y*x 的二维数组,值为点值
        double[][] _gridData = trainData.getData();
        // X轴集合，等值面插值点数量,越多越精细也越慢
        double[] _X = trainData.get_X();
        // Y轴集合，等值面插值点数量,越多越精细也越慢
        double[] _Y = trainData.get_Y();
        double _undefData = -999.0;
        int nc = dataInterval.length;
        int[][] S1 = new int[_gridData.length][_gridData[0].length];
        // 获取轮廓
        List<Border> _borders = Contour.tracingBorders(_gridData, _X, _Y, S1, _undefData);
        // 生成等值线
        List<PolyLine> cPolylineList = Contour.tracingContourLines(_gridData, _X, _Y, nc, dataInterval, _undefData,
                _borders, S1);
        // 平滑
        cPolylineList = Contour.smoothLines(cPolylineList);
        // 生成等值面
        List<Polygon> cPolygonList = Contour.tracingPolygons(_gridData, cPolylineList, _borders, dataInterval);
        // 等值面结果转换
        return FeatureUtil.getFeatureCollection(cPolygonList);
    }

    /**
     * 生成等值面
     * 需要做网格计算，并IDW插值计算
     *
     * @param trainData    等值点数据，3*n 的二维数组,{0,{x,y,v}}按顺序是"x"组，"y"组,"值"组
     * @param dataInterval 数据分层级别
     */
    public FeatureCollection equiSurfaceGrid(TrainData trainData, double[] dataInterval) throws IOException,
            SchemaException {
        double[][] sourceData = trainData.getData();
        //等值面插值点数量,越多越精细也越慢
        int[] size = trainData.getSize();
        double _undefData = -999.0;
        int width = size[0], height = size[1];
        double[] _X = new double[width];
        double[] _Y = new double[height];

        // 获取shp边界，生成格网点
        double minX = 106.55;
        double minY = 17.7;
        double maxX = 119.75;
        double maxY = 28.0;
        Interpolate.createGridXY_Num(minX, minY, maxX, maxY, _X, _Y);
        int nc = dataInterval.length;
        // IDW插值格网点
        double[][] _gridData = Interpolate.interpolation_IDW_Neighbor(sourceData, _X, _Y, 12, _undefData);
        int[][] S1 = new int[_gridData.length][_gridData[0].length];
        // 获取轮廓
        List<Border> _borders = Contour.tracingBorders(_gridData, _X, _Y, S1, _undefData);
        // 生成等值线
        List<PolyLine> cPolylineList = Contour.tracingContourLines(_gridData, _X, _Y, nc, dataInterval, _undefData,
                _borders, S1);
        // 平滑
        cPolylineList = Contour.smoothLines(cPolylineList);
        // 生成等值面
        List<Polygon> cPolygonList = Contour.tracingPolygons(_gridData, cPolylineList, _borders, dataInterval);
        // 等值面结果转换
        return FeatureUtil.getFeatureCollection(cPolygonList);
    }
}
